//
//  PracticeCollectionViewController.swift
//  XingCeHelper
//
//  Created by 申铭 on 2022/12/5.
//

import UIKit

let kBenchSectionInsetLeft: CGFloat = 20

class PracticeCollectionViewController: BaseCollectionViewController {
    
    private var viewModel = PracticeCollectionViewModel()
    private var datas = [BaseSectionModel]()
    
    init() {
        let layout = UICollectionViewFlowLayout()
        let width = (kScreenWidth - 2 * kBenchSectionInsetLeft - 15) / CGFloat(2)
        layout.itemSize = CGSize(width: width, height: 45)
        // line跟滚动方向相同的间距
        layout.minimumLineSpacing = 15
        // item跟滚动方向垂直的间距
        layout.minimumInteritemSpacing = 15
        // 滚动方向
        layout.scrollDirection = .vertical
        layout.sectionInset = UIEdgeInsets(top: 15, left: 20, bottom: 15, right: 20)
        // headerView的宽高
        layout.headerReferenceSize = CGSize(width: kScreenWidth, height: 40)
        super.init(collectionViewLayout: layout)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        title = "练习"
        
        datas = viewModel.loadDatas()
        // 数据不满一屏时支持滚动
        collectionView.alwaysBounceVertical = true
        collectionView.register(UINib(nibName: kPracticeCollectionViewCell, bundle: nil), forCellWithReuseIdentifier: kPracticeCollectionViewCell)
        collectionView.register(UINib(nibName: kPracticeCollectionReusableView, bundle: nil), forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: kPracticeCollectionReusableView)
        navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(self.importIdiom))
    }
    
    @objc private func importIdiom() {
        navigationController?.pushViewController(FuncationListViewController(), animated: true)
    }

    override func numberOfSections(in collectionView: UICollectionView) -> Int {
        return datas.count
    }

    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return datas[section].items.count
    }
    
    override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        var headerView: PracticeCollectionReusableView!
        if kind == UICollectionView.elementKindSectionHeader {
            headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: kPracticeCollectionReusableView, for: indexPath as IndexPath) as? PracticeCollectionReusableView
            if indexPath.section == 0 {
                headerView.titleLabel.text = "记忆"
            } else {
                headerView.titleLabel.text = "计算"
            }
        }
        return headerView
    }

    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: kPracticeCollectionViewCell, for: indexPath) as! PracticeCollectionViewCell
        cell.model = datas[indexPath.section].items[indexPath.row] as? PracticeCollectionCellModel
        return cell
    }
    
    override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let type = datas[indexPath.section].items[indexPath.row] as? PracticeCollectionCellModel
        if indexPath.section == 0 {
            let vc = ReciteViewController()
            vc.type = type?.type ?? .reciteIdiom
            navigationController?.pushViewController(vc, animated: true)
        } else {
            let vc = PracticeController()
            vc.type = type?.type ?? .twoDigitSubtraction
            navigationController?.pushViewController(vc, animated: true)
        }
    }

}
